use clap::Parser;
use crate::aof::AofType;

#[derive(Debug, Copy, Clone)]
pub enum RedisMode {
    Standalone,
    Cluster,
}

#[derive(Debug, Parser, Clone)]
#[command(name = "redis-server", version, author, about, long_about = "None")]
pub struct RedisConfig {
    #[arg(short, long, default_value = "7379")]
    pub port: String,

    #[arg(short, long, default_value = "standalone", value_parser = parse_format)]
    pub mode: RedisMode,

    #[arg( long, default_value = "./aof_log")]
    pub aof_path: String,

    #[arg( long, default_value = "EverySec", value_parser = parse_aof)]
    pub aof_type: AofType,

}


fn parse_format(format: &str) -> Result<RedisMode, String> {
    let temp = format.to_uppercase();
    let format = temp.as_str();
    match format {
        "STANDALONE" => Ok(RedisMode::Standalone),
        "CLUSTER" => Ok(RedisMode::Cluster),
        _ => return Err(format!("Invalid format: {}", format))
    }
}


fn parse_aof(t: &str) -> Result<AofType, String> {
    let temp = t.to_uppercase();
    let format = temp.as_str();
    match format {
        // "NO" => Ok(AofType::No),
        "ALWAYS" => Ok(AofType::Always),
        "EVERYSEC" => Ok(AofType::EverySec),
        _ => return Err(format!("Invalid aof type: {}", format))
    }
}
